package com.blacksheep.mtm.controller;

import com.blacksheep.mtm.common.utils.ResultUtil;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.common.Result;
import com.blacksheep.mtm.pojo.manager.TbBoxDetails;
import com.blacksheep.mtm.pojo.manager.TbDevice;
import com.blacksheep.mtm.service.BindBoxService;
import com.blacksheep.mtm.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@RestController
@Api("冰包绑定记录")
public class BindBoxController {

    @Autowired
    private BindBoxService boxService;


    @RequestMapping(value = "/bind/box/list",method = RequestMethod.GET)
    @ApiOperation(value = "绑定列表")
    public DataTablesResult getWSDeviceList(int draw, int start, int length, String searchKey, int[] state,
                                          String minDate, String maxDate,
                                          @RequestParam("search[value]") String search,
                                          @RequestParam("order[0][column]") int orderCol,
                                          @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","id", "box_code","", "create_time","state"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= boxService.getList(draw,start,length,state,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }

    @RequestMapping(value = "/bind/box/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增绑定，批量")
    public Result<Integer> insert(String codes){
        String[] list = codes.split("\n");
        int result = boxService.addBatch(Arrays.asList(list));
        return new ResultUtil<Integer>().setData(result);
    }


    @RequestMapping(value = "/bind/box/delete/{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除")
    public Result<Integer> delBind(@PathVariable Integer id){
        Integer result = boxService.del(id);
        return new ResultUtil<Integer>().setData(result);
    }

    @RequestMapping(value = "/bind/box/unbind/{id}",method = RequestMethod.POST)
    @ApiOperation(value = "解绑")
    public Result<Integer> unbind(@PathVariable Integer id){
        Integer result = boxService.unbind(id);
        return new ResultUtil<Integer>().setData(result);
    }

    @RequestMapping(value = "/bind/box/details/add",method = RequestMethod.POST)
    @ResponseBody
    public Result<TbBoxDetails> addBoxDetails(int bindId,String code){
        TbBoxDetails result =   boxService.addDetails(bindId,code);
        return new ResultUtil<TbBoxDetails>().setData(result);
    }

    @RequestMapping(value = "/bind/box/details/delete",method = RequestMethod.DELETE)
    @ResponseBody
    public Result<Integer> deletBoxDetails(Long[] ids){
        int count =  boxService.delDetails(ids);
        return new ResultUtil<Integer>().setData(count);
    }


    @RequestMapping(value = "/bind/box/remark",method = RequestMethod.POST)
    @ResponseBody
    public Result<Integer> addRemark(int id,String remark){
        int count =  boxService.addRemark(id,remark);
        return new ResultUtil<Integer>().setData(count);
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
